package com.example.wechat;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.R;
import com.example.wechat.adapter.ImageGridAdapter;
import com.example.wechat.model.Comment;
import com.example.wechat.model.Moment;
import com.example.wechat.utils.ImageLoader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class MomentAdapter extends RecyclerView.Adapter<MomentAdapter.ViewHolder> {
    private Context context;
    private List<Moment> moments;
    private OnMomentClickListener listener;
    private SimpleDateFormat dateFormat;

    public interface OnMomentClickListener {
        void onMomentClick(Moment moment);
        void onImageClick(Moment moment, int position);
        void onLikeClick(Moment moment);
        void onCommentClick(Moment moment);
    }

    public MomentAdapter(Context context, List<Moment> moments, OnMomentClickListener listener) {
        this.context = context;
        this.moments = moments;
        this.listener = listener;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_moment, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Moment moment = moments.get(position);
        
        holder.usernameTextView.setText(moment.getUserName());
        holder.timeTextView.setText(dateFormat.format(new Date(moment.getTime())));
        ImageLoader.loadImage(context, moment.getUserAvatar(), holder.avatarImageView);

        if (moment.getImages() != null && !moment.getImages().isEmpty()) {
            holder.imageRecyclerView.setVisibility(View.VISIBLE);
            ImageGridAdapter imageAdapter = new ImageGridAdapter(moment.getImages(), (view, position1) -> {
                if (listener != null) {
                    listener.onImageClick(moment, position1);
                }
            });
            holder.imageRecyclerView.setAdapter(imageAdapter);
            holder.imageRecyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        } else {
            holder.imageRecyclerView.setVisibility(View.GONE);
        }

        holder.contentTextView.setText(moment.getContent());
        if (moment.getLocation() != null && !moment.getLocation().isEmpty()) {
            holder.locationTextView.setVisibility(View.VISIBLE);
            holder.locationTextView.setText(moment.getLocation());
        } else {
            holder.locationTextView.setVisibility(View.GONE);
        }

        // 设置点赞和评论
        holder.likeButton.setSelected(moment.isLiked());
        holder.likeButton.setOnClickListener(v -> {
            if (listener != null) {
                listener.onLikeClick(moment);
            }
        });

        holder.commentButton.setOnClickListener(v -> {
            if (listener != null) {
                listener.onCommentClick(moment);
            }
        });

        // 显示点赞列表
        if (moment.getLikes() != null && !moment.getLikes().isEmpty()) {
            holder.likesList.setVisibility(View.VISIBLE);
            StringBuilder likesText = new StringBuilder();
            for (String like : moment.getLikes()) {
                if (likesText.length() > 0) {
                    likesText.append(", ");
                }
                likesText.append(like);
            }
            holder.likesList.setText(likesText.toString());
        } else {
            holder.likesList.setVisibility(View.GONE);
        }

        // 显示评论列表
        if (moment.getComments() != null && !moment.getComments().isEmpty()) {
            holder.commentsList.setVisibility(View.VISIBLE);
            StringBuilder commentsText = new StringBuilder();
            for (Comment comment : moment.getComments()) {
                if (commentsText.length() > 0) {
                    commentsText.append("\n");
                }
                commentsText.append(comment.getUserName()).append(": ").append(comment.getContent());
            }
            holder.commentsList.setText(commentsText.toString());
        } else {
            holder.commentsList.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return moments.size();
    }

    public void updateMoments(List<Moment> newMoments) {
        this.moments = newMoments;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView avatarImageView;
        TextView usernameTextView;
        TextView timeTextView;
        TextView contentTextView;
        TextView locationTextView;
        RecyclerView imageRecyclerView;
        ImageView likeButton;
        ImageView commentButton;
        TextView likesList;
        TextView commentsList;

        ViewHolder(View itemView) {
            super(itemView);
            avatarImageView = itemView.findViewById(R.id.avatar_image);
            usernameTextView = itemView.findViewById(R.id.username_text);
            timeTextView = itemView.findViewById(R.id.time_text);
            contentTextView = itemView.findViewById(R.id.content_text);
            locationTextView = itemView.findViewById(R.id.location_text);
            imageRecyclerView = itemView.findViewById(R.id.image_recycler_view);
            likeButton = itemView.findViewById(R.id.like_button);
            commentButton = itemView.findViewById(R.id.comment_button);
            likesList = itemView.findViewById(R.id.likes_list);
            commentsList = itemView.findViewById(R.id.comments_list);
        }
    }
} 